package com.example.yeujian5.yuejian5.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yeujian5.yuejian5.R;
import com.example.yeujian5.yuejian5.activity.PictureInfoActivity;
import com.example.yeujian5.yuejian5.bean.PictureBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by 7h0rns on 2017/10/11.
 */

public class PictureAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;//承载上下文
    private List<PictureBean> mDataList;//动态数组承载数据使用
    private LayoutInflater mLayoutInflater;

    public PictureAdapter(Context mContext, List<PictureBean> mDataList) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = mLayoutInflater.from(parent.getContext()).inflate(R.layout.picture_item, parent, false);
        return new PictureAdapter.ViewHolder(v);
    }

    //将数据绑定到控件
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final PictureBean entity = mDataList.get(position);
        if (null == entity)
            return;
        PictureAdapter.ViewHolder viewHolder = (PictureAdapter.ViewHolder) holder;
//        viewHolder.name.setText(entity.getUserid());
        if (entity.getShaier() == null) {
            viewHolder.name.setText("cxz");
        } else {
            viewHolder.name.setText(entity.getShaier().getUser_name());
        }
        viewHolder.hint.setText(entity.getHits());
        viewHolder.time.setText(entity.getUpdate_time());
        if (!entity.getThumb0().isEmpty()) {
            Picasso.with(mContext).load("http://172.24.10.175/workout/Uploads/" + entity.getThumb0()).into(viewHolder.img);
        } else {
            viewHolder.img.setImageResource(R.drawable.qya);
        }
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, PictureInfoActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("id", entity.getId());
                intent.putExtra("title", entity.getTitle());
//                intent.putExtra("UserName", entity.getShaier().getUser_name());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    //找到控件，将其初始化
    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView name, hint, time;

        public ViewHolder(View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.photo_pic);
            hint = (TextView) itemView.findViewById(R.id.photo_hint);
            name = (TextView) itemView.findViewById(R.id.photo_user);
            time = (TextView) itemView.findViewById(R.id.photo_time);
        }
    }
}